package com.zlop.sys.controller;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.nyvi.support.entity.Pagination;
import com.zlop.cache.RedisCacheClient;
import com.zlop.common.dto.Result;
import com.zlop.sys.model.SysUserDO;
import com.zlop.sys.query.SysUserQuery;
import com.zlop.sys.service.SysUserService;

/**
 * 管理控制层
 *
 * @author  曾令鹏
 * @since   jdk1.8
 * @version 2018年4月9日 曾令鹏
 */
@Controller
public class ManageController {
	
	@Autowired
	private RedisCacheClient redisCacheClient;

	@Autowired
	private SysUserService sysUserService;
	
	/**
	 * 主页
	 * @return
	 */
	@GetMapping(value = { "main.html", "/" })
	public ModelAndView mian() {
		ModelAndView mv = new ModelAndView("manage/main");
		SysUserQuery query = new SysUserQuery();
		List<SysUserDO> list = sysUserService.getList(query, Pagination.startPage(1, 1));
		if (!list.isEmpty()) {
			mv.addObject("user", list.get(0));
		}
		return mv;
	}
	
	@ResponseBody
	@GetMapping(value = "/incr", produces={"application/json;", "text/html;charset=UTF-8;"})
	public Result incr(String num) {
		long incr = redisCacheClient.incr("abc", NumberUtils.toInt(num));
		return Result.success("变更成功", incr);
	}
	
	
	@ResponseBody
	@GetMapping("/uid")
	public String uid(HttpSession session) {
		redisCacheClient.set("zlop", "nice", 10);
        UUID uid = (UUID) session.getAttribute("uid");
        System.out.println(session.getAttribute("sessionId"));
        if (uid == null) {
            uid = UUID.randomUUID();
        }
        String object = redisCacheClient.get("zlop");
        System.err.println(object);
        session.setAttribute("uid", uid);
        session.setAttribute("uid2", "123456");
        return session.getId();
    }
}
